package dyy.zc.sportruler.ui.activity;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.ArrayList;
import java.util.List;

import dyy.zc.sportruler.R;
import dyy.zc.sportruler.base.BaseActivity;
import dyy.zc.sportruler.bean.PlanInfo;
import dyy.zc.sportruler.bean.SportName;
import dyy.zc.sportruler.ui.adapter.MakePlanAdapter;
import dyy.zc.sportruler.utils.BmobUtils;
import dyy.zc.sportruler.utils.MakePlanUtils;

public class MyPlanActivity extends BaseActivity implements View.OnClickListener {
    private TextView title,ok;
    private RelativeLayout rl_warmUpView, rl_mainActionView, rl_relaxActionView;
    private TextView tv_warmUp, tv_mainAction, tv_relaxAction;
//    private ImageView iv_warmUpAdd, iv_mainActionAdd, iv_relaxActionAdd;
    private ListView warmUpListView, mainActionListView, relaxActionListView;
    private List<SportName> warmUpList = new ArrayList<>();
    private List<SportName> mainActionList = new ArrayList<>();
    private List<SportName> relaxActionList = new ArrayList<>();
    //创建适配器
    private MakePlanAdapter warmUpAdapter;
    private MakePlanAdapter mainActionAdapter;
    private MakePlanAdapter relaxActionAdapter;
//    private MakePlanUtils makePlanUtils;
    private ImageView scanner;
    private BmobUtils bmobUtils;
    private List<PlanInfo> list = new ArrayList<>();
    @Override
    protected void findViewByIDS() {
        title = findViewsById(R.id.top_tv_title);
        ok = findViewsById(R.id.top_tv_right);
        rl_warmUpView = findViewsById(R.id.base_day_rl_warmUp);
        rl_mainActionView = findViewsById(R.id.base_day_rl_mainAction);
        rl_relaxActionView = findViewsById(R.id.base_day_rl_relaxAction);

        tv_warmUp = findViewsById(R.id.makePlan_tv_warmUp);
//        iv_warmUpAdd = findViewsById(R.id.makePlan_iv_warmUpAdd);
        warmUpListView = findViewsById(R.id.makePlan_listView_warmUp);

        tv_mainAction = findViewsById(R.id.makePlan_tv_mainAction);
//        iv_mainActionAdd = findViewsById(R.id.makePlan_iv_mainActionAdd);
        mainActionListView = findViewsById(R.id.makePlan_listView_mainAction);

        tv_relaxAction = findViewsById(R.id.makePlan_tv_relaxAction);
//        iv_relaxActionAdd = findViewsById(R.id.makePlan_iv_relaxActionAdd);
        relaxActionListView = findViewsById(R.id.makePlan_listView_relaxAction);
        scanner = findViewsById(R.id.scanner);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_plan);
        initData();
        bind();
    }

    private void bind() {
        scanner.setOnClickListener(this);
    }


    private void initData() {

        bmobUtils = new BmobUtils(this);
        if (getIntent().getIntExtra("type",0)==1){
            String name = getIntent().getStringExtra("student");
            scanner.setVisibility(View.GONE);
            //获取计划
            dialog = ProgressDialog.show(this,null,"正在加载计划.....");
            bmobUtils.getPlanInfo(list,loginuser.getId(),name,1,handler);
        }
        title.setText("健身计划");
        warmUpAdapter = new MakePlanAdapter(this,warmUpList);
        mainActionAdapter = new MakePlanAdapter(this,mainActionList);
        relaxActionAdapter = new MakePlanAdapter(this,relaxActionList);
        warmUpListView.setAdapter(warmUpAdapter);
        mainActionListView.setAdapter(mainActionAdapter);
        relaxActionListView.setAdapter(relaxActionAdapter);
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1://获取计划--学员详情信息
                    setListViewData();
                    break;
            }
        }
    };

    public void setListViewData(){
        PlanInfo planInfo = list.get(0);
        warmUpList.addAll(planInfo.getWarmList());
        mainActionList.addAll(planInfo.getMainList());
        relaxActionList.addAll(planInfo.getRelaxList());
        setListViewHeight(warmUpListView);
        setListViewHeight(mainActionListView);
        setListViewHeight(relaxActionListView);
        warmUpAdapter.setList(warmUpList);
        mainActionAdapter.setList(mainActionList);
        relaxActionAdapter.setList(relaxActionList);
        warmUpListView.setVisibility(View.VISIBLE);
        mainActionListView.setVisibility(View.VISIBLE);
        relaxActionListView.setVisibility(View.VISIBLE);
        dialog.dismiss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.scanner:
//                if (EasyPermissions.hasPermissions(getActivity(), Manifest.permission.CAMERA)) {
                    Intent intent = new Intent(this, ScannerActivity.class);
//                    MyBombUtils.COUNT = 0;
                    startActivityForResult(intent, 456);
//                }
                break;
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 456:
                if (data != null){
                    Bundle bundle = data.getExtras();
                    if (bundle == null) {
                        return;
                    }
                    if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                        dialog = ProgressDialog.show(this, null, "正在加载数据...");
                        String scanner_id = bundle.getString(CodeUtils.RESULT_STRING);
                        Toast.makeText(this, "获取成功", Toast.LENGTH_SHORT).show();
                        //搜寻健身计划
                        Log.e("scanner_id",scanner_id);
                        warmUpList.clear();
                        mainActionList.clear();
                        relaxActionList.clear();
                        bmobUtils.getPlanInfo(list,scanner_id,1,handler);
//                        CollectPlan.userSportPlan.setStudentId(scanner_id);
//                        bombUtils.queryStudent(scanner_id);
                    } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                        Toast.makeText(this, "获取失败", Toast.LENGTH_SHORT).show();
                    }
                }

                break;
        }
    }

    /**
     * 由于ScrollView中嵌套ListView显示的时候，高度只显示一行数据的高度
     * 所以应该计算ListView的原本高度，使用getCount()*item.getMeasuredHeight()
     * 计算高度，重新设置ListView的高度
     *
     * @param listView
     */
    public void setListViewHeight(ListView listView) {
        // 获取ListView对应的Adapter
        MakePlanAdapter adapter = (MakePlanAdapter) listView.getAdapter();
        if (adapter == null) {
            return;
        }
        int totalHeight = 0;
        int len = adapter.getCount();
        View item = adapter.getView(0, null, listView);
        item.measure(0, 0);
        totalHeight = item.getMeasuredHeight() * len;
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight;
        listView.setLayoutParams(params);
    }
}
